/**
  * The Window in which the game is displayed
  *
  * @author "Brian MacCarthy","Owen Grogan";
  * @created "2007-03-04 Brian MacCarthy";
  * @revised "$Date: 2007-03-06 19:11:00 $"; 
  */


package exfist;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GameWindow extends Canvas implements KeyListener
{
	JFrame main_window;
	Intro intro;
	int introcount;
	Arena arena;
	Round r;
	boolean isIntroOn=true;
	private final int Height = 768;
	private final int Width = 1024;
	Player playa,playa2;
	BufferStrategy bs;
	Music m;
	/*
	 * the main game loop
	 * @param args
	 */
	public static void main(String[] args)
	{
		GameWindow gw=new GameWindow();
		gw.play();

	}
	/**
	 * default constructor
	 * creates two players and sets them up.
	 * sets up the jframe
	 * and plays the music
	 *
	 */
	private GameWindow()
	{
		playa = new Player(1);
		playa2 = new Player(2);
		playa2.setPos(674, 450);
		playa.setOpponent(playa2);
		playa2.setOpponent(playa);
		arena=new Arena();
		intro=new Intro();
		r=new Round();
		JFrame wayFrame=new JFrame("The Way of the Exploding Fist");
		JPanel wayPanel=(JPanel)wayFrame.getContentPane();
		setBounds(0, 0, Width, Height);
		wayPanel.setPreferredSize(new Dimension(Width, Height));
		wayPanel.setLayout(null);
		wayPanel.add(this);
		wayFrame.setBounds(50,50,Width,Height);
		wayFrame.setVisible(true);
		wayFrame.setResizable(false);
		wayFrame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		m= new Music();
		m.playMusic();
		addKeyListener(this);
		createBufferStrategy(2);
		bs=getBufferStrategy();
	}
	/**
	 * draws the graphics,will flip the players based on the position of player 1
	 *
	 */
	public void paintIT(){
		Graphics2D g = (Graphics2D)bs.getDrawGraphics();
		if(isIntroOn){
			g.drawImage(intro.getCurrentImage(),0,0,this);
			g.dispose();
		}else{
			g.drawImage(arena.getArenaBackground(), 0, 0, 1024, 768, this);
			if(playa.isLeftOf(playa2))
			{
				drawNorm(playa,g);
				drawNorm(playa2,g);
			}
			else
			{
				drawFlipped(playa,g);
				drawFlipped(playa2,g);
			}
			
		}
		r.paintStatus(g);
		bs.show();
	}
	/**
	 * draws the player facing the default way
	 * @param p the player
	 * @param g the graphics
	 */
	private void drawNorm(Player p,Graphics2D g)
	{
		g.drawImage(p.getCurrentSprite(),p.getX(),p.getY(),null);
	}
	/**
	 * draws the player backwards
	 * @param p the player
	 * @param g the graphics
	 */
	private void drawFlipped(Player p,Graphics2D g)
	{
		g.drawImage(p.getCurrentSprite(), 
				p.getX(), p.getY(), (p.getX()+p.getCurrentSprite().getWidth()), (p.getY()+p.getCurrentSprite().getHeight()),
				p.getCurrentSprite().getWidth(), 0, 0, p.getCurrentSprite().getHeight(), null);
	}
	/**
	 * the main game loop
	 *
	 */
	public void play(){
		while(isVisible()){
			paintIT();
			if(!isIntroOn){
				r.time();
				playa.repaint();
			}
		}
	}
	/**
	 * detects the key presses and acts accordingly
	 */
	public void keyPressed(KeyEvent e) 
	{
		if(playa.current.isBusy()==true)
		{
			//System.out.println("Doing Something");
		}
		else
		{
			//System.out.println("should be busy now");
			switch(e.getKeyCode())
			{
				//Pressing enter will start the game
				case KeyEvent.VK_ENTER : isIntroOn = false;m.stopMusic();m.setMusic("Audio/ExplodingFistPlay.mid");m.playMusic();break;
				case KeyEvent.VK_R: r.resetTime();break;
				case KeyEvent.VK_D: playa.movef();break;
				case KeyEvent.VK_A: playa.moveb();break;
				case KeyEvent.VK_P: playa.punchstr();break;
				case KeyEvent.VK_O: playa.punchmed();break;
				case KeyEvent.VK_I: playa.punchwk();break;
				case KeyEvent.VK_L: playa.kickstr();break;
				case KeyEvent.VK_K: playa.kickmed();break;
				case KeyEvent.VK_J: playa.kickwk();break;
				case KeyEvent.VK_E: playa.jumpf();break;
				case KeyEvent.VK_W: playa.hop();break;
				case KeyEvent.VK_Q: playa.jumpb();break;
				case KeyEvent.VK_S: playa.setKeyHeld(true);playa.duck();break;
				case KeyEvent.VK_B: playa.setKeyHeld(true);playa.high_block();break;
				case KeyEvent.VK_M: playa2.AI();break;
			}
		}
	}
	public void keyReleased(KeyEvent e) 
	{
		//System.out.println("key released");
		switch(e.getKeyCode())
		{
			case KeyEvent.VK_D: playa.setKeyHeld(false);break;
			case KeyEvent.VK_B: playa.setKeyHeld(false);break;
		}
	}
	public void keyTyped(KeyEvent e) 
	{
		
	}
}